package com.test28_类库应用;

import org.junit.Test;

import java.util.Arrays;

class Student implements Comparable<Student> {
    private String name;
    private int age;
    private double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "【Student】name = " + this.name + ", age = " + this.age + ", score = " + this.score;
    }

    @Override
    public int compareTo(Student o) {
        if (this.score > o.score) {
            return -1;
        } else if (this.score < o.score) {
            return 1;
        } else {
            return this.age - o.age;
        }
    }
}

interface IDataHandle {
    public Student[] getData(); // 定义最终的返回数据
}

class StringDataHandle implements IDataHandle {
    private Student[] students; // 定义对象数组
    private String value;

    public StringDataHandle(String value) {
        this.value = value;
        this.handle();
    }

    private void handle() {
        String[] result = this.value.split("\\|");  // 拆分数据
        this.students = new Student[result.length];
        for (int x = 0; x < result.length; x++) {
            String[] data = result[x].split(":");
            this.students[x] = new Student(data[0], Integer.parseInt(data[1]), Double.parseDouble(data[2]));
        }
    }

    @Override
    public Student[] getData() {
        Arrays.sort(this.students);
        return this.students;
    }
}

class DataHandleFactory {
    private DataHandleFactory() {
    }

    public static IDataHandle getInstance(String data) {
        return new StringDataHandle(data);
    }
}

/**
 * 数据排序
 */
public class YootkDemo_07 {

    @Test
    public void test1() {
        String str = "张三:21:98|李四:22:89|王五:20:70|赵六:19:70";
        IDataHandle handle = DataHandleFactory.getInstance(str);
        Arrays.stream(handle.getData()).forEach(System.out::println);
    }
}
